﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ApiDoc
{
    /// <summary>
    /// Api Documentation for a Event.
    /// </summary>
    public class EventDocumentation : MemberDocumentationBase<EventInfo>
    {
        internal EventDocumentation(EventInfo member, ApiDocumentationManager manager)
            : base(member, manager)
        {
        }


        public string Name
        {
            get
            {
                return Manager.CodeSyntaxGenerator.GenerateName(Member);
            }
        }

        public string CodeDefinition
        {
            get
            {
                return Manager.CodeSyntaxGenerator.GenerateCodeDefinition(Member);
            }
        }

        /// <summary>
        /// Gets the parent type documentation this member is contained within.
        /// </summary>
        public TypeDocumentation ParentType
        {
            get
            {
                if (m_parentType == null) {
                    m_parentType = Manager.GetTypeDocumentation(Member.ReflectedType);
                }

                return m_parentType;
            }
        }

        private TypeDocumentation m_parentType;

        /// <summary>
        /// Gets the type documentation for the type this member was declared within.
        /// </summary>
        /// <remarks>
        /// This is the class the developer actually wrote the code of this method within.
        /// </remarks>
        public TypeDocumentation DeclaringType
        {
            get
            {
                if (m_declaringType == null) {
                    m_declaringType = Manager.GetTypeDocumentation(Member.DeclaringType);
                }

                return m_declaringType;
            }
        }

        private TypeDocumentation m_declaringType;


        /// <summary>
        /// Gets the type documentation for the event handler type this of this event.
        /// </summary>
        public TypeDocumentation EventHandlerType
        {
            get
            {
                if (m_eventHandlerType == null) {
                    if (Member.EventHandlerType == null) {
                        return null;
                    }

                    m_eventHandlerType = Manager.GetTypeDocumentation(Member.EventHandlerType);
                }

                return m_eventHandlerType;
            }
        }

        private TypeDocumentation m_eventHandlerType;


        /// <summary>
        /// Gets the basic modifiers.
        /// </summary>
        public string Modifiers
        {
            get
            {
                if ((Member.GetAddMethod().Attributes & MethodAttributes.Public) != 0) {
                    if (Member.GetAddMethod().IsStatic) {
                        return "public static";
                    } else {
                        return "public";
                    }
                } else if ((Member.GetAddMethod().Attributes & MethodAttributes.Private) != 0) {
                    if (Member.GetAddMethod().IsStatic) {
                        return "private static";
                    } else {
                        return "private";
                    }
                } else if ((Member.GetAddMethod().Attributes & MethodAttributes.PrivateScope) != 0) {
                    if (Member.GetAddMethod().IsStatic) {
                        return "private static";
                    } else {
                        return "private";
                    }
                } else if ((Member.GetAddMethod().Attributes & MethodAttributes.FamANDAssem) != 0) {
                    if (Member.GetAddMethod().IsStatic) {
                        return "protected internal static";
                    } else {
                        return "protected internal";
                    }
                } else if ((Member.GetAddMethod().Attributes & MethodAttributes.FamORAssem) != 0) {
                    if (Member.GetAddMethod().IsStatic) {
                        return "protected internal static";
                    } else {
                        return "protected internal";
                    }
                } else if ((Member.GetAddMethod().Attributes & MethodAttributes.Family) != 0) {
                    if (Member.GetAddMethod().IsStatic) {
                        return "protected static";
                    } else {
                        return "protected";
                    }
                } else if ((Member.GetAddMethod().Attributes & MethodAttributes.Assembly) != 0) {
                    if (Member.GetAddMethod().IsStatic) {
                        return "internal static";
                    } else {
                        return "internal";
                    }
                }

                return String.Empty;
            }
        }
    }
}
